<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\Tenant\AuthController;
use App\Http\Controllers\Tenant\TaskController;
use App\Http\Controllers\Tenant\TaskApplicationController;
use App\Http\Controllers\Tenant\FreelancerController;
use App\Http\Controllers\Tenant\SettlementController;
use App\Http\Controllers\Tenant\SubSettlementController;
use App\Http\Controllers\Tenant\RechargeController;
use App\Http\Controllers\Tenant\TransactionController;
use App\Http\Controllers\Tenant\InvoiceController;
use App\Http\Controllers\Tenant\DashboardController;
use App\Http\Controllers\Tenant\TaskServiceTypeController;
use App\Http\Controllers\Tenant\BillingInfoController;

/*
|--------------------------------------------------------------------------
| Tenant Routes
|--------------------------------------------------------------------------
|
| Here is where you can register tenant routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

Route::get('/', function () {
    return response()->json([
        'message' => 'Tenant API',
        'tenant_type' => request()->attributes->get('tenant_type')
    ]);
});

// 租户端API路由组
Route::prefix('api')->group(function () {
    // 租户认证路由
    Route::prefix('tenant')->group(function () {
        Route::get('/', function () {
            return response()->json([
                'message' => 'Tenant API',
            ]);
        });
        
        // 公共路由（无需认证）
        Route::post('login', [AuthController::class, 'login']);
        Route::post('register', [AuthController::class, 'register']);
        Route::post('send-sms-code', [AuthController::class, 'sendSmsCode']);
        
        // 需要认证的路由
        Route::middleware(['auth:sanctum'])->group(function () {
            Route::get('me', [AuthController::class, 'me']);
            Route::get('codes', [AuthController::class, 'codes']);
            Route::post('change-password', [AuthController::class, 'changePassword']);
            Route::post('logout', [AuthController::class, 'logout']);
            Route::post('logout-all', [AuthController::class, 'logoutAll']);
            Route::post('refresh-token', [AuthController::class, 'refreshToken']);

            Route::get('dashboard/overview', [DashboardController::class, 'overview']);

            // 任务服务类型
            Route::get('task-service-types', [TaskServiceTypeController::class, 'index']);

            // 任务管理路由
            Route::prefix('tasks')->group(function () {
                Route::get('/', [TaskController::class, 'index']);
                Route::post('/', [TaskController::class, 'store']);
                Route::get('/{task}', [TaskController::class, 'show']);
                Route::put('/{task}', [TaskController::class, 'update']);
                Route::post('/{task}/status', [TaskController::class, 'updateStatus']);
                Route::get('/{task}/qr-code', [TaskController::class, 'qrCode']);
                Route::delete('/{task}', [TaskController::class, 'destroy']);
                Route::post('/{task}/qr-code', [TaskController::class, 'generateQrCode']);
                Route::get('/export', [TaskController::class, 'export']);
                Route::get('/{task}/applications', [TaskApplicationController::class, 'index']);
                Route::get('/{task}/applications/export', [TaskApplicationController::class, 'export']);
                Route::get('/{task}/applications/export-settlement', [TaskApplicationController::class, 'exportSettlementTemplate']);
                Route::post('/{task}/applications/{application}/approve', [TaskApplicationController::class, 'approve']);
                Route::post('/{task}/applications/{application}/reject', [TaskApplicationController::class, 'reject']);
                Route::post('/{task}/applications/{application}/dimission', [TaskApplicationController::class, 'dimission']);
                Route::put('/applications/{application}', [TaskApplicationController::class, 'update']);
            });
            
            // 自由职业者管理路由
            Route::prefix('freelancers')->group(function () {
                Route::get('/', [FreelancerController::class, 'index']);
                Route::get('/{freelancer}', [FreelancerController::class, 'show']);
                Route::get('/export', [FreelancerController::class, 'export']);
            });
            
            // 结算管理路由
            Route::prefix('settlements')->group(function () {
                Route::get('/', [SettlementController::class, 'index']);
                Route::post('/', [SettlementController::class, 'store']);
                Route::post('/{settlementNumber}/batch-pay', [SettlementController::class, 'batchPay']);
                Route::post('/{settlementId}/trigger-tax', [SettlementController::class, 'triggerTax']);
                Route::get('/{settlement}', [SettlementController::class, 'show']);
                Route::put('/{settlement}', [SettlementController::class, 'update']);
                Route::delete('/{settlement}', [SettlementController::class, 'destroy']);
                Route::get('/export', [SettlementController::class, 'export']);
            });

            // 子结算单管理路由
            Route::prefix('sub-settlements')->group(function () {
                Route::get('/', [SubSettlementController::class, 'index']);
                Route::post('/', [SubSettlementController::class, 'store']);
                Route::get('/status-options', [SubSettlementController::class, 'getStatusOptions']);
                Route::get('/statistics', [SubSettlementController::class, 'getStatistics']);
                Route::get('/{subSettlement}', [SubSettlementController::class, 'show']);
                Route::put('/{subSettlement}', [SubSettlementController::class, 'update']);
                Route::delete('/{subSettlement}', [SubSettlementController::class, 'destroy']);
                Route::post('/{subSettlement}/verify', [SubSettlementController::class, 'verify']);
                Route::post('/{subSettlement}/submit', [SubSettlementController::class, 'submit']);
                Route::post('/{subSettlement}/distribute', [SubSettlementController::class, 'distribute']);
                Route::post('/{subSettlement}/cancel', [SubSettlementController::class, 'cancel']);
                Route::post('/{subSettlement}/refund', [SubSettlementController::class, 'refund']);
                Route::post('/batch-action', [SubSettlementController::class, 'batchAction']);
            });
            // 充值管理路由
            Route::prefix('recharges')->group(function () {
                Route::get('/', [RechargeController::class, 'index']);
                Route::post('/', [RechargeController::class, 'store']);
                Route::get('/export', [RechargeController::class, 'export']);
                Route::get('/{recharge}', [RechargeController::class, 'show']);
            });

            // 交易账单路由
            Route::prefix('transactions')->group(function () {
                Route::get('/', [TransactionController::class, 'index']);
                Route::get('/sub-types', [TransactionController::class, 'getSubTypes']);
                Route::get('/export', [TransactionController::class, 'export']);
                Route::get('/{transaction}', [TransactionController::class, 'show']);
            });

            // 发票管理路由
            Route::prefix('invoices')->group(function () {
                Route::get('/', [InvoiceController::class, 'index']);
                Route::post('/', [InvoiceController::class, 'store']);
                Route::get('/billing-type-options', [InvoiceController::class, 'billingTypeOptions']);
                Route::get('/billing-category-options', [InvoiceController::class, 'billingCategoryOptions']);
                Route::get('/status-options', [InvoiceController::class, 'statusOptions']);
                Route::get('/{invoice}/download', [InvoiceController::class, 'download']);
                Route::get('/{invoice}', [InvoiceController::class, 'show']);
                Route::put('/{invoice}', [InvoiceController::class, 'update']);
                Route::delete('/{invoice}', [InvoiceController::class, 'destroy']);
            });

            // 落地公司选项
            Route::prefix('landing-companies')->group(function () {
                Route::get('/options', function () {
                    return response()->json([
                        'code' => 0,
                        'message' => '获取落地公司选项成功',
                        'data' => \App\Models\LandingCompany::getActiveCompanies()
                    ]);
                });
                Route::get('/billing-categories', function () {
                    return response()->json([
                        'code' => 0,
                        'message' => '获取落地公司开票类目成功',
                        'data' => \App\Models\LandingCompany::getAllBillingCategories()
                    ]);
                });
            });

            // 租户开票信息
            Route::prefix('billing-info')->group(function () {
                Route::get('/', [BillingInfoController::class, 'show']);
                Route::post('/', [BillingInfoController::class, 'store']);
            });

            // 个人信息
            Route::get('profile', 'Tenant\ProfileController@show');
            Route::put('profile', 'Tenant\ProfileController@update');
        });
    });
});
